const {
	CAPTCHA_SCENE,
	EMAIL_SCENE,
	LOG_TYPE
} = require('../../common/constants')
const {
	verifyEmailCode
} = require('../../lib/utils/verify-code')
/**
 * 判断邮箱验证码是否正确
 */
module.exports = async function(params = {}) {
	const schema = {
		email: 'email',
		code: 'string',
		inviteCode: {
			required: false,
			type: 'string'
		}
	}
	this.middleware.validate(params, schema); //?(中间件的作用)判断数据结构是否匹配
	const {
		email,
		code,
		inviteCode
	} = params
	try {
		// 验证邮箱验证码，验证不通过时写入失败日志
		await verifyEmailCode({
			email,
			code,
			scene: EMAIL_SCENE.REGISTER
		})
	} catch (error) {
		await this.middleware.uniIdLog({ //验证失败写入日志表
			data: {
				email
			},
			type: LOG_TYPE.REGISTER,
			success: false
		})
		throw error //抛出错误
	}
	return {
		errCode:0,
		mailCode:code
	}
}

